package com.chnbst.bstyun.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.eos.ExcelExport;
import com.chnbst.bstyun.model.pojo.OperateLog;
import com.chnbst.bstyun.service.OperateLogService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author zhangq
 *
 */
@Controller
@RequestMapping("/sysAdmin")
public class SysLogAdminController {

	//每页显示的用户数
	private int pageSize = 10;
	//用户列表页码
	private int currPage = 1;
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private OperateLogService operateLogService;
	@Resource 
	private TaskExecutor taskExecutor;//注入Spring封装的异步执行器
	
	
	/**
	 * 操作日志查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/actionLogQuery",method=RequestMethod.GET)
	public ModelAndView actionLogQuery()throws Exception{
		return new ModelAndView("/admin/systemManager/actionLogQuery");
	}
	/**
	 * Ajax 查询操作日志list
	 * @return 操作日志列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/operateLogList/search",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goodsList(String page,String last, HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
	
		String startTime = request.getParameter("starttime");
		String endTime = request.getParameter("endtime");
		String operateName = request.getParameter("username");
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("operateName", operateName);
		
		int totalRows = operateLogService.selectOperateLogCount(map);
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "operate_time");
		map.put("sortOrder", "desc");
		
		List<OperateLog> operateLogList = operateLogService.selectOperateLogList(map);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("operateLogList", operateLogList);
		return resultMap;
	}
	
	// 导出日志列表（POI方式）
	@RequestMapping(value ="/logReport",method = RequestMethod.GET)
	public void logReport(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(SignUtil.isNotEmpty(endTime)) {
			endTime += " 23:59:59";
		}
		
		String fileName = "日志.xls";
//		String fileName = request.getParameter("name");
		int fromIndex = 1;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		map.put("sortField", "operate_time");
		map.put("sortOrder", "desc");
		List<OperateLog> list = operateLogService.selectOperateLogList(map);

		// 进行数据转换
		List<OperateLog> listr = new ArrayList<OperateLog>();
		//excel表格title项
		String[] title = { "操作人", "操作时间", "操作IP", "操作内容"};
		for (OperateLog log : list) {
			OperateLog ol = new OperateLog();

			ol.setOperateName(log.getOperateName());
			ol.setOperateTime(log.getOperateTime());
			ol.setLoginIp(log.getLoginIp());
			ol.setOperateContent(log.getOperateContent());

			listr.add(ol);
		}
		
		try {
			new ExcelExport<OperateLog>(OperateLog.class).exportExcel(request, response, listr, fileName, fromIndex, title);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
